# coding=utf-8
import sys
from function_utils import Global, Common


def route_info():
    cmd = "ip r"
    info = Common.execute_cmd(cmd)[1]
    return info


def add_route(dest, gate):
    cmd = '''ip r add {0} via {1}'''.format(dest, gate)
    ret, out, err = Common.execute_cmd(cmd)
    print(out)
    print(err)


def del_route(dest, gate):
    cmd = '''ip r del {0} via {1}'''.format(dest, gate)
    ret, out, err = Common.execute_cmd(cmd)
    print(out)
    print(err)


def write_widgets_value(value):
    data = [
        {"MultiInfoText": value},
        {},
        {},
        {}
    ]
    Global.values_write(data)
    Global.report_info(100)


def main():
    if sys.argv[1] == 'pre':
        write_widgets_value(route_info())
    elif sys.argv[1] == 'start':
        data = Global.values_parse(sys.argv[2])
        dest = data[2]['Entry'].strip()
        gate = data[3]['Entry'].strip()
        if data[1]['Combobox'].strip() == 'Add':
            add_route(dest, gate)
        else:
            del_route(dest, gate)
        write_widgets_value(route_info())


if __name__ == '__main__':
    main()

